package com.yand.thinkingInJava.thinkingInJava21.threadNewcomponent;


import java.util.concurrent.*;

class DragonBall implements Runnable{

    private int id ;
    private CyclicBarrier cyclicBarrier;

    public DragonBall(int id,  CyclicBarrier cyclicBarrier){
        this.id = id;
        this.cyclicBarrier = cyclicBarrier;
    }


    @Override
    public void run() {
        try {
            TimeUnit.MILLISECONDS.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("已收集第"+id+"号龙珠！");

        try {
            cyclicBarrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }

    }
}


public class CyclicBarrierDemo {

    public static final int TOTAL = 7;

    public static void main(String [] args) throws BrokenBarrierException, InterruptedException {

        CyclicBarrier cyclicBarrier = new CyclicBarrier(TOTAL);


        ExecutorService exec = Executors.newCachedThreadPool();

        for(int i = 1 ; i <= TOTAL; i ++){
            exec.execute(new DragonBall(i, cyclicBarrier));

        }
        System.out.println("hahahaha");

        exec.shutdown();


    }



}
